//
//  SYJComposeTextView.swift
//  weibo
//
//  Created by 孙英建 on 16/4/23.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class SYJComposeTextView: UITextView {
    
    // 外界给字体设置大小
    override var font: UIFont?{
        didSet{
            placeholderLabel.font = font
        }
    }
    
    // 外界设置Placeholder文字
    var placeholderStr: String?{
        didSet{
            placeholderLabel.text = placeholderStr
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // 设置UI
    func setUpUI(){
        self.backgroundColor = UIColor.whiteColor()
        
        // 添加控件
        addSubview(placeholderLabel)
        
        // 添加约束
        placeholderLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(8)
            make.leading.equalTo(5)
            make.width.equalTo(kWidth - 10)
        }
        
        // 通知监听文本中的文字输入
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewTextDidChange", name: UITextViewTextDidChangeNotification, object: nil)
        
    }
    
    // 通知的监听方法
    @objc private func textViewTextDidChange(){
        placeholderLabel.hidden = self.hasText()
    }
    
    
    // MARK: - 懒加载控件
    private lazy var placeholderLabel: UILabel = {
        let pdl = UILabel()
        pdl.font = UIFont.systemFontOfSize(12)
        pdl.textColor = UIColor.grayColor()
        pdl.numberOfLines = 0
        pdl.alpha = 0.3
        pdl.text = "大家好我是占位符"
        return pdl
    }()
    
    
    
    // MARK: - 移除通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}















